<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 实现数组的reduce方法
        Array.prototype.myReduce = function (arr,callback, initialValue) {
            var num = initialValue ? initialValue : arr[0];
            var i = initialValue?0:1;
            for (i; i < arr.length; i++) {
                num = callback(num, arr[i], i);
            }
            return num
        }
        function sum (a,b) {
            return a + b
        }
        var arr = [1,2,3,4,5]
        console.log(arr.myReduce(arr,sum,10))
        console.log(arr.myReduce(arr,sum))
    </script>
</body>
</html>